package com.xhb.ui.collect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xhb.EasyBrowserApplication;
import com.xhb.biz.po.CollectGroupPO;
import com.xhb.biz.po.CollectUrlPO;
import com.xhb.biz.po.ProjectUrlPO;
import com.xhb.biz.service.CollectGroupService;
import com.xhb.biz.service.CollectUrlService;
import com.xhb.biz.service.ProjectUrlService;
import com.xhb.ui.TabFactory;
import com.xhb.ui.resources.Resources;
import com.xhb.ui.tools.UItools;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.noear.solon.Solon;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.List;

@Data
public class CollectGroupButton extends JButton {
    /**
     * 项目id
     */
    private String projectId;

    /**
     * 收藏夹标题
     */
    private String title;

    /**
     * 收藏夹id
     */
    private String collectGroupId;

    /**
     * 是否是项目
     */
    private boolean isProject;

    private CollectBar collectBar;

    public CollectGroupButton(boolean isProject, String title, CollectBar collectBar, String collectGroupId) {
        this.isProject = isProject;
        this.title = title;
        this.collectGroupId = collectGroupId;
        this.collectBar = collectBar;
        setIcon(Resources.getIcon("/collect/folder.png"));
        UItools.parseButton(this);
        setBackground(Color.WHITE);

        //鼠标移动上来变色
        final JButton jButton = this;
        setOpaque(true);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {  //鼠标移上去
                jButton.setBackground(Color.lightGray);
            }
        });
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {  //鼠标离开
                jButton.setBackground(Color.WHITE);
            }
        });
        //移动变色end

        JButton that = this;
        addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                createPopMenu().show(that, 0, that.getY() + 18);
            }
        });
        if (!isProject) {
            addMouseListener(new GroupPopMenu(collectGroupId, this.title, collectBar, this));
        }
    }


    public JPopupMenu createPopMenu() {
        JPopupMenu popupMenu = new JPopupMenu();
        if (this.isProject) {
            ProjectUrlService projectUrlService = Solon.context().getBean(ProjectUrlService.class);
            List<ProjectUrlPO> urlPos = projectUrlService.list(new LambdaQueryWrapper<ProjectUrlPO>().eq(ProjectUrlPO::getProjectId, projectId)
                    .orderByAsc(ProjectUrlPO::getSortNo));
            JMenuItem menuItem;
            for (ProjectUrlPO urlPo : urlPos) {
                menuItem = new JMenuItem(urlPo.getName());
                String icon = urlPo.getIcon();
                if (StringUtils.isBlank(icon)) {
                    menuItem.setIcon(Resources.getIcon("/collect/browser.png"));
                } else {
                    ImageIcon imageIcon = UItools.base64ToImageIcon(icon);
                    if (null != UItools.base64ToImageIcon(icon)) {
                        menuItem.setIcon(imageIcon);
                    } else {
                        menuItem.setIcon(Resources.getIcon("/collect/browser.png"));
                    }
                }
//                menuItem.addActionListener(new ActionListener() {
//                    public void actionPerformed(ActionEvent e) {
//                        EasyBrowserApplication.insertTab(TabFactory.createTab(urlPo.getUrl(),
//                                EasyBrowserApplication.getEngine().newBrowser()));
//                    }
//                });
//                popupMenu.add(menuItem);
            }
        } else {
            CollectUrlService collectUrlService = Solon.context().getBean(CollectUrlService.class);
            List<CollectUrlPO> urlPos = collectUrlService.list(new LambdaQueryWrapper<CollectUrlPO>()
                    .eq(CollectUrlPO::getCollectGroupId, collectGroupId)
                    .orderByAsc(CollectUrlPO::getSortNo));
            JMenuItem menuItem;
            for (CollectUrlPO urlPo : urlPos) {
                menuItem = new JMenuItem(urlPo.getName());
                String icon = urlPo.getIcon();
                if (StringUtils.isBlank(icon)) {
                    menuItem.setIcon(Resources.getIcon("/collect/browser.png"));
                } else {
                    ImageIcon imageIcon = UItools.base64ToImageIcon(icon);
                    if (null != UItools.base64ToImageIcon(icon)) {
                        menuItem.setIcon(imageIcon);
                    } else {
                        menuItem.setIcon(Resources.getIcon("/collect/browser.png"));
                    }
                }
//                //为按钮创建一个右键菜单
//                menuItem.addMouseListener(new MouseAdapter() {
//                    public void mouseReleased(MouseEvent e) {
//                        //判断事件是鼠标右键事件
//                        if (e.getButton() == MouseEvent.BUTTON3) {
//                            UpdateCollectUrlDialog updateCollectUrlDialog = new UpdateCollectUrlDialog(urlPo, collectBar, true);
//                            updateCollectUrlDialog.setModal(true);
//                            updateCollectUrlDialog.setVisible(true);
//                        } else {
//                            EasyBrowserApplication.insertTab(TabFactory.createTab(urlPo.getUrl(),
//                                    EasyBrowserApplication.getEngine().newBrowser()));
//                        }
//                    }
//                });
                popupMenu.add(menuItem);
            }
        }
        return popupMenu;
    }


    @AllArgsConstructor
    class GroupPopMenu extends MouseAdapter {

        private String collectGroupId;

        private String title;

        private CollectBar collectBar;

        private JButton jButton;

        public void mousePressed(MouseEvent e) {
            int mods = e.getModifiers();
            if ((mods & InputEvent.BUTTON3_MASK) != 0) {
                JPopupMenu popupMenu = new JPopupMenu();
                JMenuItem delete = new JMenuItem();
                delete.setText("删除");
                delete.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int opt = JOptionPane.showConfirmDialog(null, "确认删除" + title + "吗？",
                                "请确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                        if (opt == JOptionPane.YES_NO_OPTION) {
                            CollectGroupService collectGroupService = Solon.context().getBean(CollectGroupService.class);
                            collectGroupService.delete(collectGroupId);
                            jButton.setVisible(false);
                            //do删除操作
                            collectBar.refresh();
                        }
                    }
                });

                popupMenu.add(delete);
                JMenuItem edit = new JMenuItem();
                edit.setText("编辑");
                edit.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String newGroupName = JOptionPane.showInputDialog(null, "请输入新的分组名称", title);
                        CollectGroupService collectGroupService = Solon.context().getBean(CollectGroupService.class);
                        CollectGroupPO collectGroupPO = collectGroupService.getById(collectGroupId);
                        collectGroupPO.setName(newGroupName);
                        collectGroupService.update(collectGroupPO);
                        jButton.setVisible(false);
                        collectBar.refresh();
                    }
                });

                popupMenu.add(edit);
                popupMenu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    }
}
